package com.micro.modual.extension;

import com.micro.common.MicroConstant;
import com.micro.common.biz.GeneratorCodeUtil;
import com.micro.common.biz.TreeUtil;
import com.micro.common.util.ObjectUtil;
import com.micro.common.util.SnowFlakeUtil;
import com.micro.modual.entity.SystemDept;
import com.micro.modual.model.bo.systemdept.SystemDeptBO;
import com.micro.modual.model.tree.SystemDeptTreeDTO;
import org.apache.commons.collections.CollectionUtils;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * <h1>系统部门表 延展类</h1>
 *
 * @author LevelCoder
 * @version V1.0.0
 * @email LevelCoder@126.com
 * @date 2020-08-04 00:23:26
 */
public class SystemDeptExtension {

    /**
     * <h2>转换: 系统部门表 新增</h2>
     *
     * @param systemDeptBO {@link SystemDeptBO} 系统部门表 新增/修改 请求对象
     * @return {@link SystemDept} 系统部门表
     */
    public static SystemDept converAddSystemDept(SystemDeptBO systemDeptBO,Integer deptLevel) {
        SystemDept systemDept = new SystemDept();
        systemDept.setId(SnowFlakeUtil.getId());
        systemDept.setDelFlag(systemDeptBO.getDelFlag());
        systemDept.setDeptCode(GeneratorCodeUtil.generatorSystemCode());
        systemDept.setDeptLevel(deptLevel);
        systemDept.setDeptName(systemDeptBO.getDeptName());
        systemDept.setDeptSort(ObjectUtil.isNotEmpty(systemDeptBO.getDeptSort()) ? systemDeptBO.getDeptSort(): 0);
        systemDept.setParentCode(systemDeptBO.getParentCode());
        systemDept.setCreateTime(new Date());
        return systemDept;
    }

    /**
     * <h2>转换: 系统部门表 修改</h2>
     *
     * @param systemDeptBO {@link SystemDeptBO} 系统部门表 新增/修改 请求对象
     * @return {@link SystemDept} 系统部门表
     */
    public static SystemDept converEditSystemDept(SystemDeptBO systemDeptBO, Integer deptLevel) {
        SystemDept systemDept = new SystemDept();
        systemDept.setId(systemDeptBO.getId());
        systemDept.setDelFlag(systemDeptBO.getDelFlag());
        systemDept.setDeptLevel(deptLevel);
        systemDept.setDeptName(systemDeptBO.getDeptName());
        systemDept.setDeptSort(ObjectUtil.isNotEmpty(systemDeptBO.getDeptSort()) ? systemDeptBO.getDeptSort(): 0);
        systemDept.setParentCode(systemDeptBO.getParentCode());
        systemDept.setUpdateTime(new Date());
        return systemDept;
    }

    /**
     * <h2>转换部门树</h2>
     * @param systemDeptList
     * @return
     */
    public static List<SystemDeptTreeDTO> converSystemDeptTree(List<SystemDept> systemDeptList) {
        List<String> deptParentCode = new ArrayList<>();
        List<SystemDeptTreeDTO> systemDeptTreeDTOList = new ArrayList<>();
        if(CollectionUtils.isNotEmpty(systemDeptList)){
            for(SystemDept systemDept:systemDeptList){
                SystemDeptTreeDTO systemDeptTreeDTO = new SystemDeptTreeDTO();
                systemDeptTreeDTO.setId(systemDept.getId());
                systemDeptTreeDTO.setDelFlag(systemDept.getDelFlag());
                systemDeptTreeDTO.setCreateTime(systemDept.getCreateTime());
                systemDeptTreeDTO.setDeptLevel(systemDept.getDeptLevel());
                systemDeptTreeDTO.setDeptSort(systemDept.getDeptSort());
                systemDeptTreeDTO.setDeptName(systemDept.getDeptName());
                systemDeptTreeDTO.setParentName(systemDept.getParentName());
                systemDeptTreeDTO.setUpdateTime(systemDept.getUpdateTime());
                systemDeptTreeDTO.setCode(systemDept.getDeptCode());
                systemDeptTreeDTO.setParentCode(systemDept.getParentCode());
                deptParentCode.add(systemDept.getParentCode());
                systemDeptTreeDTOList.add(systemDeptTreeDTO);
            }
        }
        if(deptParentCode.contains(MicroConstant.SystemDefaultValue.DEFAULT_PARENT_CODE)){
            return TreeUtil.bulid(systemDeptTreeDTOList, MicroConstant.SystemDefaultValue.DEFAULT_PARENT_CODE);
        }else {
            return systemDeptTreeDTOList;
        }
    }
}
